<template>
  <el-card>
    <div class="" slot="header"><span>账号列表</span></div>
    <el-table
      ref="tableRef"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <!-- 多选 -->
      <el-table-column type="selection" width="55"></el-table-column>
      <!-- 账号 -->
      <el-table-column prop="account" label="姓名"></el-table-column>
      <!-- 用户组 -->
      <el-table-column prop="userGroup" label="用户组"></el-table-column>
      <!-- 日期 -->
      <el-table-column label="日期">
        <template slot-scope="scope">
          {{ scope.row.ctime | formatYMD }}
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column width="160" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <!-- 气泡确认 -->
          <el-popconfirm
            @confirm="handleDelete(scope.row)"
            title="这是一段内容确定删除吗？"
          >
            <el-button slot="reference" size="mini" type="danger">
              删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="mt20"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 5]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <!-- 操作 -->
    <div class="mt20">
      <el-button @click="handleBatchDel"  type="danger">批量删除</el-button>
      <el-button @click="cancelChanges" type="primary">取消选择</el-button>
    </div>
  </el-card>
</template>

<script>
//解构接口
import { getUserListReq, delUserReq,batchDelUserReq } from "@/api/user";

export default {
  data() {
    return {
      //表格数据
      tableData: [],
      //id组成的
      ids: [],
      //分页数据
      currentPage: 1,
      pageSize: 3,
      total: 0,
    };
  },
  //方法
  methods: {
   
    //获取数据函数
    async getData() {
      //提取数据
      let currentPage = this.currentPage;
      let pageSize = this.pageSize;
      //发送请求
      let res = await getUserListReq({
        currentPage,
        pageSize,
      });
      //解构数据
      let { data, total } = res.data;
      //数据赋值
      this.total = total;
      this.tableData = data;
    },

    handleSelectionChange(val) {
      
      this.multipleSelection = val;
      this.ids = val.map(v=>v.id)

    },
       cancelChanges(){
    if (this.ids.length) {
        this.$refs.tableRef.clearSelection()
        this.$message.info('取消成功')
      } else {
        this.$message.info('没有选中')
      }
  },
    handleEdit(index, row) {
      console.log(index, row);
    },
    //删除
    async handleDelete(row) {
      // console.log(row)
      //获取id
      let { id } = row;
      //发送请求
      let res = await delUserReq({ id });
      //解构数据
      let { code, msg } = res.data;
      if (code === 0) {
        //拉取数据
        this.getData();
      }
    },
    //改变页码
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    //改变当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
    //批量删除
    handleBatchDel(){
    this.$confirm('此操作将永久删除选中用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      .then(async ()=>{
        let res = await batchDelUserReq({ids:this.ids})
        let {code,msg} = res.data
        if (code=== 0) {
          //重新拉取数据
          this.getData()
          this.$message.success(msg)
        }
      }).catch(()=>{
          this.$message({
            type:'info',
            message:'已取消删除'
          })
        })
    }
  },

  //created 生命周期初始化发请求
  created() {
    this.getData();
  },
};
</script>

<style lang="scss" scoped></style>
